package com.dianrui.agent.fragment;

import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.activity.LoginActivity;
import com.dianrui.agent.activity.MessageNewActivity;
import com.dianrui.agent.activity.SettingActivity;
import com.dianrui.agent.activity.SevenDataAnalysisActivity;
import com.dianrui.agent.activity.StatementAccountsActivity;
import com.dianrui.agent.activity.StationRankActivity;
import com.dianrui.agent.event.RfreshMy;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.dianrui.agent.view.pulltoRefresh.PullToRefreshBase;
import com.dianrui.agent.view.pulltoRefresh.PullToRefreshScrollView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMainMy extends BaseFragment {

    @BindView(R.id.act_my_msg_num)
    TextView actMyMsgNum;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_earnings)
    TextView tvEarnings;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.my_scrollview)
    PullToRefreshScrollView myScrollView;


    @Override
    public int getContentViewId() {
        return R.layout.fragment_main_my;
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        getBalance();
        myScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        myScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getBalance();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getBalance();
            }
        });

    }

    private void getBalance() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl() + Url.BALANCE, "", new OkHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result) && isAdded()) {
                    myScrollView.onRefreshComplete();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            tvBalance.setText(jsonObject.optJSONObject("data").optString("earnings"));
                            tvEarnings.setText(jsonObject.optJSONObject("data").optString("income"));
                            tvWithdraw.setText(jsonObject.optJSONObject("data").optString("order_number"));
                            if (jsonObject.optJSONObject("data").optInt("unread_number") > 0) {
                                int showNumber = jsonObject.optJSONObject("data").optInt("unread_number");
                                actMyMsgNum.setText(showNumber + "");
                                actMyMsgNum.setVisibility(View.VISIBLE);
                                if (showNumber > 99) {
                                    actMyMsgNum.setVisibility(View.VISIBLE);
                                    actMyMsgNum.setText("99+");
                                }
                            } else {
                                actMyMsgNum.setVisibility(View.GONE);
                            }

                        } else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        } else if ("1002".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                            SharedUtil.setToken("");
                            spUtils.clear();
                            JumpActivitys(LoginActivity.class);
                            getActivity().finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshMy rfreshMy) {
        getBalance();
    }

    @OnClick({R.id.message_img, R.id.setting_img, R.id.click_1, R.id.click_2,R.id.click_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.message_img://消息
                //JumpActivitys(MessageActivity.class);
                JumpActivitys(MessageNewActivity.class);
                break;
            case R.id.setting_img://设置
                JumpActivitys(SettingActivity.class);
                break;


            case R.id.click_1://对账单
                JumpActivitys(StatementAccountsActivity.class);
                break;
            case R.id.click_2://数据分析
                JumpActivitys(SevenDataAnalysisActivity.class);
                break;

            case R.id.click_3://站点排行
                JumpActivitys(StationRankActivity.class);
                break;

            default:
                break;

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
